package fr.greta.centreFormation.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

import fr.greta.centreFormation.bean.Cursus;
import fr.greta.centreFormation.bean.Domaine;
import fr.greta.centreFormation.bean.UniteEnseignement;
import fr.greta.centreFormation.dao.Bdd;
import fr.greta.centreFormation.dao.CursusHome;
import fr.greta.centreFormation.dao.DomaineHome;
import fr.greta.centreFormation.dao.UniteEnseignementHome;

/**
 * Servlet implementation class ModifierCursus
 */
public class ModifierCursus extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifierCursus() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int idCursus = Integer.parseInt(request.getParameter("cursus"));
		
		Bdd base = new Bdd();
		Cursus leCursus = CursusHome.selectUnCursusById(idCursus);
		Domaine leDomaine = DomaineHome.selectUnDomaineByIdCursus(leCursus.getIdCursus()); 
		
		HttpSession session = request.getSession(true);
		
		Hashtable<Integer, String> lesUeDuCursus = new Hashtable<Integer, String>();
		
		for(UniteEnseignement u : leCursus.getAllUE()){
			lesUeDuCursus.put(u.getIdUniteEnseignement(), u.getIntituleUniteEnseignement());
		}
		
		System.out.println(lesUeDuCursus.get(1));
		
		
		session.setAttribute("intitule", leCursus.getIntituleCursus());
		session.setAttribute("codeRome", leCursus.getCodeRomeCursus());
		session.setAttribute("description", leCursus.getDescriptionCursus());
		session.setAttribute("lesUe", lesUeDuCursus);
		session.setAttribute("idDomaine", leDomaine.getIdDomaine());
		session.setAttribute("idCursus", idCursus);

		
		ArrayList<UniteEnseignement> collectionUe = UniteEnseignementHome.selectAllUniteEnseignement();
		ArrayList<Domaine> collectionDomaines = DomaineHome.selectAllDomaine();

		session.setAttribute("collectionUe", collectionUe);
		session.setAttribute("collectionDomaine", collectionDomaines);
		
		session.setAttribute("idAction", 2);
		
		request.getRequestDispatcher("formulaireCursus.jsp").forward(request, response);

		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
